//
//  ZTabbarController.m
//  ZPlayer
//
//  Created by Zed Link on 26/11/2016.
//  Copyright © 2016 ZED. All rights reserved.
//

#import "ZTabbarController.h"
#import "ZNavigationController.h"
#import <objc/runtime.h>
#import <objc/message.h>

@interface ZTabbarController (){
    
}

@end

@implementation ZTabbarController

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    [[UITabBar appearance] setTintColor:Lignt_BG_Color];
    [[UITabBar appearance] setBarTintColor:SEPARATOR_BG_COLOR];
    [UITabBar appearance].translucent = NO;
    // ========== 添加TaabarItem ==========
    
    NSArray *imageNames = @[
                            @"icon_tsb_nor",
                            @"icon_zt_nor",
                            @"icon_yy_nor",
                            @"icon_pmd_nor",
                            @"icon_gd_nor"
                            ];
    NSArray *highLightImageNames = @[
                                     @"icon_tsb_press",
                                     @"icon_zt_press",
                                     @"icon_yy_press",
                                     @"icon_pmd_press",
                                     @"icon_gd_press"
                                     ];
    
    NSArray *classNames = @[
                            @"ZHomeVC",
                            @"ZThemeVC",
                            @"ZMusicVC",
                            @"ZLightVC",
                            @"ZMeVC"
                            ];
    
    [self setupTabbarItemrWithTitles:nil images:imageNames highlightImages:highLightImageNames andClassNames:classNames];
    
    // 更新学生数据
//    [[ZUserInfo sharedInstance] updaloadData];
}

- (void)setupTabbarItemrWithTitles:(NSArray *)titles images:(NSArray <NSString *> *)imageNames highlightImages:(NSArray <NSString *> *)highLightImageNames andClassNames:(NSArray <NSString *> *)classNames
{
    // ========== 参数检查 ==========
//    BOOL condition = (titles.count == imageNames.count && classNames.count == titles.count);
//    NSString *paraErrDesc = [NSString stringWithFormat:@"%@ : TabbarItem的配置参数个数不一致", NSStringFromClass(self.class)];
//    NSAssert(condition, paraErrDesc);
    
   
    // ========== 配置TabbarItem ==========
    NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:classNames.count];
    NSString *clsErrDesc = [NSString stringWithFormat:@"%@ : 找不到CalssName对应的类, 或该类并不是控制器", NSStringFromClass(self.class)];
    
    for (int i = 0; i < classNames.count; i++)
    {
        NSString *classStr = classNames[i];
        Class cls = NSClassFromString(classStr);
        UIImage *image = [UIImage imageNamed:imageNames[i]];
        UIImage *highLightImage = [UIImage imageNamed:highLightImageNames[i]];
        
        // ========== 配置控制器 ==========
        UIViewController *vc = [[cls alloc] init];
        NSAssert([vc isKindOfClass:[UIViewController class]], clsErrDesc);
        
        // 添加Navgation
        ZNavigationController *nav = [[ZNavigationController alloc] initWithRootViewController:vc];
        
//        nav.tabBarItem.title = titles[i];
        if (image == nil) {
            ZLog(@"找不到对应的图片: %@", imageNames[i]);
        } else {
            [nav.tabBarItem setImage:image];
        
            [nav.tabBarItem setSelectedImage:highLightImage];
        }
        
        [tempArrM addObject:nav];
    }
    
    
    
    self.viewControllers = [tempArrM copy];
}

@end
